package com.ruoyi.web.controller.xiaomi;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.bye.xiaomi_mall.domain.TdOrder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.bye.xiaomi_mall.domain.TdOrderDetail;
import com.bye.xiaomi_mall.service.ITdOrderDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单详情Controller
 *
 * @author ruoyi
 * @date 2025-03-16
 */
@RestController
@RequestMapping("/xiaomi_mall/detail")
public class TdOrderDetailController extends BaseController {
    @Autowired
    private ITdOrderDetailService tdOrderDetailService;

    /**
     * 查询订单详情列表
     */
    @PreAuthorize("@ss.hasPermi('xiaomi_mall:detail:list')")
    @GetMapping(value = "list")
    public TableDataInfo list(TdOrderDetail tdOrderDetail) {
        startPage();
        List<TdOrderDetail> list = tdOrderDetailService.selectTdOrderDetailList(tdOrderDetail);
        System.out.println("进来查了");
        return getDataTable(list);
    }


    @PreAuthorize("@ss.hasPermi('xiaomi_mall:detail:list')")
    @GetMapping(value = "selectOrderDetail")
//    @GetMapping("selectOrderDetail/{id}")
    public Map<String, Object> selectOrderDetail(TdOrder tdOrder) {
        String orderCode = tdOrder.getOrderCode();
        Map<String, Object> map = new HashMap<>();
        List<TdOrderDetail> list = tdOrderDetailService.selectOrderDetail(orderCode);
//        System.out.println(list);
        map.put("list", list);
        return map;
    }

    @RequestMapping(value = "selectTdOrderDetailByCodeId")
    public AjaxResult selectTdOrderDetailByCodeId(@RequestBody TdOrderDetail orderDetail) {
         List<TdOrderDetail> orderDetailList= tdOrderDetailService.selectTdOrderDetailByCodeId(orderDetail.getOrderCode());
        return AjaxResult.success().put("orderDetailList",orderDetailList);
    }

    /**
     * 导出订单详情列表
     */
    @PreAuthorize("@ss.hasPermi('xiaomi_mall:detail:export')")
    @Log(title = "订单详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TdOrderDetail tdOrderDetail) {
        List<TdOrderDetail> list = tdOrderDetailService.selectTdOrderDetailList(tdOrderDetail);
        ExcelUtil<TdOrderDetail> util = new ExcelUtil<TdOrderDetail>(TdOrderDetail.class);
        util.exportExcel(response, list, "订单详情数据");
    }

    /**
     * 获取订单详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('xiaomi_mall:detail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tdOrderDetailService.selectTdOrderDetailById(id));
    }

    /**
     * 新增订单详情
     */
    @PreAuthorize("@ss.hasPermi('xiaomi_mall:detail:add')")
    @Log(title = "订单详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TdOrderDetail tdOrderDetail) {
        return toAjax(tdOrderDetailService.insertTdOrderDetail(tdOrderDetail));
    }

    /**
     * 修改订单详情
     */
    @PreAuthorize("@ss.hasPermi('xiaomi_mall:detail:edit')")
    @Log(title = "订单详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TdOrderDetail tdOrderDetail) {
        return toAjax(tdOrderDetailService.updateTdOrderDetail(tdOrderDetail));
    }

    /**
     * 删除订单详情
     */
    @PreAuthorize("@ss.hasPermi('xiaomi_mall:detail:remove')")
    @Log(title = "订单详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tdOrderDetailService.deleteTdOrderDetailByIds(ids));
    }
}
